package com.Ajava1704.for并发包下的类.ForComplableFuable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 作者: LCG
 * 日期: 2020/6/23 20:54
 * 描述:
 */
public class TestComplable {
    public static String  str="";
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //完成第一个线程然后完成第二个线程
        /*CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return "OK1";
        }).thenAccept(y -> {
            str=y+"-----2--------";
        });*/

        //任务的合并  合并前边的任务的值
        CompletableFuture.supplyAsync(()->{
            System.out.println("========1");

            return "--1--";
        }).thenCombine(CompletableFuture.supplyAsync(()->{return "--2--";}),(x,y)->{
            System.out.println("========2");
            return x+y;
        }).thenCombine(CompletableFuture.supplyAsync(()->{return "--3--";}),(x1,y1)->{
            System.out.println("========3");
            return x1+y1;
        }).thenAccept(x3->{
            System.out.println("=====-----=="+x3);
        });

        //所有的任务中最先完成任务的一个

        CompletableFuture<String> t1 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK1";
        });

        CompletableFuture<String> t2 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK2";
        });

        CompletableFuture<String> t3 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "OK3";
        });

       /* CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(t1, t2, t3);

        System.out.println(objectCompletableFuture.get());*/

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(t1, t2, t3);

        //如果所有的任务不执行完毕的话 就阻塞该步骤，直到执行完毕
        System.out.println(voidCompletableFuture.get());

        System.out.println("============");
    }


}
